<template>
  <div class="table-box">
    <ProTable ref="proRef" :columns="columns" :pagination="false" :request-api="list">
      <template #tableHeader="scope">
        <el-button type="primary" @click="opera(undefined)">创建特色</el-button>
        <el-button type="danger" @click="deleteItem(scope.selectedListIds)" :disabled="scope.selectedListIds.length === 0"
          >批量删除</el-button
        >
      </template>
      <template #showInSearch="{ row }">
        <el-tag type="success" v-show="row.showInSearch">是</el-tag>
        <el-tag type="danger" v-show="!row.showInSearch">否</el-tag>
      </template>
      <template #opera="{ row }">
        <el-button type="primary" link :icon="EditPen" @click="opera(row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteItem([row.id])">删除</el-button>
      </template>
    </ProTable>
    <teseDrawer ref="teseRef"></teseDrawer>
  </div>
</template>

<script setup lang="ts">
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { ref } from "vue";
import { list, create, edit, deleteTese } from "@/api/modules/tese";
import { useHandleData } from "@/hooks/useHandleData";
import { EditPen, Delete } from "@element-plus/icons-vue";
import teseDrawer from "../components/teseDrawer.vue";

const teseRef = ref<InstanceType<typeof teseDrawer> | null>();

const proRef = ref<InstanceType<typeof ProTable> | null>();

const columns: ColumnProps[] = [
  { type: "selection", width: 80 },
  { prop: "sort", label: "排序值" },
  { prop: "name", label: "特色名称" },
  { prop: "showInSearch", label: "是否显示" },
  { prop: "opera", label: "操作" }
];

const opera = (row: any) => {
  const params: any = {
    row: row ? row : { name: "", sort: 1, showInSearch: true },
    api: row ? edit : create,
    getTable: proRef.value?.getTableList
  };
  teseRef.value?.acceptParams(params);
};

const deleteItem = async (ids: any) => {
  await useHandleData(deleteTese, { ids }, "删除所选特色");
  proRef.value?.clearSelection();
  proRef.value?.getTableList();
};
</script>

<style></style>
